package common.core
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.states.AddChild;

	public class GMouse 
	{
		private static var m_root : DisplayObject;
		private static var m_mouseHand : GSprite;
		private static var m_handCenter : Boolean;
		private static var m_handOffest : Point;
		private static var m_handData : Object;
		
		public function GMouse()
		{
			
		}
		
		public static function build(root : Sprite) : void
		{
			m_root = root;
			m_mouseHand = new GSprite;
			
   			root.addChild(m_mouseHand);
			
			root.addEventListener(Event.ENTER_FRAME,OnUpdate);
		}
		
		public static function destory() : void
		{
			m_root.removeEventListener(Event.ENTER_FRAME,OnUpdate);
			
			m_root = null;
			m_mouseHand = null;
			m_handOffest = null;
			m_handCenter = false;
			m_handData = null;
		}
		
		public static  function showHand(view : Sprite,handData:Object=null) : void
		{
			m_handData = handData;
			m_mouseHand.cleanChildren(m_mouseHand);
			m_mouseHand.visible = true;
			
			if(view)
			{
				m_mouseHand.addChild(view);
			}
		}
		
		public static function hideHand() : void
		{
			m_mouseHand.visible = false;
		}
		
		public static function set handData(data : Object) : void
		{
			m_handData = data;
		}
		
		public static function get handData() : Object
		{
			return m_handData;
		}
		
		public static function get mouseX() : Number
		{
			return m_root.mouseX;
		}
		
		public static function get mouseY() : Number
		{
			return m_root.mouseY;
		}
		
		public static function set handCenter(center : Boolean) : void
		{
			m_handCenter = center;
		}
		
		public static function getHandOffest() : Point
		{
			return m_handOffest;
		}
		
		public static function setHandOffest(offset : Point) : void
		{
			m_handOffest = offset;
		}
		
		private static function OnUpdate(evt : Event) : void
		{
			if(m_handCenter)
			{
				m_mouseHand.x = mouseX + m_mouseHand.width >> 1;
				m_mouseHand.y = mouseY + m_mouseHand.height >> 1;
			}
			else
			{
				m_mouseHand.x = mouseX;
				m_mouseHand.y = mouseY;
			}
			
			if(m_handOffest)
			{
				m_mouseHand.x += m_handOffest.x;
				m_mouseHand.y += m_handOffest.y;
			}
		}
	}
}